package com.jjb.www.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeRecyclerView;
import com.jjb.www.R;
import com.jjb.www.app.AppFragment;
import com.jjb.www.http.api.CurriculumSortApi;
import com.jjb.www.http.api.HomeCourseListApi;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.other.AppConfig;
import com.jjb.www.ui.activity.CopyActivity;
import com.jjb.www.ui.activity.CurriculumDetitleActivity;
import com.jjb.www.ui.adapter.CourseListAdapter;

/**
 * author : Android CRG
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 首页课程列表
 */
public final class CourseListFragment extends AppFragment<CopyActivity> implements BaseAdapter.OnItemClickListener {

    private ShapeRecyclerView mCourseRecv;
    private CourseListAdapter mAdapter;
    private static final String INTENT_KEY_IN_ID = "ID";

    public static CourseListFragment newInstance(String id) {
        CourseListFragment fragment = new CourseListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_KEY_IN_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.course_list_fragment;
    }

    @Override
    protected void initView() {
        mCourseRecv = findViewById(R.id.course_recv);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        mCourseRecv.setLayoutManager(gridLayoutManager);
        mAdapter = new CourseListAdapter(getContext());
        mAdapter.setOnItemClickListener(this);
        mCourseRecv.setAdapter(mAdapter);

    }

    @Override
    protected void initData() {
        getListData();
    }

    private void getListData() {
        EasyHttp.get(this)
                .api(new HomeCourseListApi()
                        .setUser_id(AppConfig.getInstance().getUserBean().getUser().getId()+"")
                        .setEssence_id(getString(INTENT_KEY_IN_ID))
                        .setPage(1)
                        .setPageSize(10))
                .request(new HttpCallback<HttpListData<HomeCourseListApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpListData<HomeCourseListApi.Bean> data) {
                        mAdapter.setData(data.getData().getList());
                    }

                });
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        Intent intent = new Intent();
        intent.putExtra("card_book_id","0");
        intent.putExtra("status","0");
        intent.putExtra("cate",mAdapter.getItem(position).getCate());
        intent.putExtra("teach_course_info_id",mAdapter.getItem(position).getId()+"");
        intent.putExtra("certificate_id","0");
        intent.putExtra("teach_complete_log_id","0");
        intent.putExtra("sentry_parent_id","0");
        intent.putExtra("sentry_id","0");
        intent.setClass(getContext(), CurriculumDetitleActivity.class);
        startActivity(intent);
    }
}